﻿Public Class ViewStore

    Private Sub ViewStore_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UI.FormatDataGrid(DataGridView1)
    End Sub
    Private Sub loaddata()
        cmd.Parameters("@S") = txtSearch.Text
        DataGridView1.DataSource = cmd.ExecuteDataTable("SELECT * FROM Store WHERE Active=1 AND UPPER(StoreName) LIKE UPPER('%'+@S+'%');")
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        loaddata()
        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim frm = New Store
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            loaddata()
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If Me.DataGridView1.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        Dim row = Me.DataGridView1.SelectedRows(0)
        Dim frm As New Store
        frm.l.Text = row.Cells("StoreId").Value
        frm.txtstore.Text = row.Cells("StoreName").Value
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            loaddata()
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Me.DataGridView1.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        If MessageBox.Show("Are you sure want to delete ?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) <> Windows.Forms.DialogResult.Yes Then
            Exit Sub
        End If
        Dim row = Me.DataGridView1.SelectedRows(0)
        Dim id = row.Cells("StoreId").Value

        cmd.Parameters("@Id") = id
        cmd.ExecuteNonQuery("UPDATE Store SET Active=0 WHERE StoreId=@Id")
        loaddata()
    End Sub
End Class